﻿using System;
using System.Net;
using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Ayatta.Contract.Domain;
using Web.Base.Extensions;
using Web.Manage.Controllers;
using Ayatta.Contract.Extension;
using System.Collections.Generic;
using Web.Manage.Areas.Game.Models;

namespace Web.Manage.Areas.Game.Controllers
{
    public class ArticleController : AdminBaseController
    {

        public ArticleController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(int page = 1, int size = 50, int? id = null, int? parentId = null, string title = null, int gameId = 0, GameArticleCategory? category = null, GameArticleStatus? status = null, int? flag = null,
                                  DateTime? date = null)
        {
            var model = new ArticleControllerModel.Index();
            var games = GetGames();
            model.Id = id;
            model.ParentId = parentId;
            model.Title = title;
            model.GameId = gameId;
            model.Category = category;
            model.Status = status;
            model.Flag = flag;
            model.Date = date;
            model.Games = games;
            model.Articles = GameService.GetPagedGameArticle(page, size, id ?? 0, parentId ?? 0, title, gameId, category, status, flag, date).ToPagedList(page, size);
            return PartialView(model);
        }


        public ActionResult Detail(int? param)
        {
            var model = new ArticleControllerModel.Detail();
            var games = GetGames();
            if (param.HasValue)
            {
                model.Article = GameService.GetGameArticle(param.Value);
            }
            else
            {
                model.Article = new GameArticle();
            }
            model.Games = games;
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Detail(int? param, GameArticle model, int[] flags)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;
            var flag = 0;
            if (flags.Length > 0)
            {
                flag = flags.Aggregate(flag, (current, next) => current | next);
            }
            model.Flag = flag;
            if (param.HasValue && param.Value > 0)
            {
                var old = GameService.GetGameArticle(param.Value);
                if (TryUpdateModel(old))
                {
                    old.Flag = flag;
                    old.ModifiedOn = dateTime;
                    var result = GameService.UpdateGameArticle(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/game/article", "/game/article") : Json(HttpStatusCode.BadRequest, "更新失败", true, "/game/article", "/game/article");
                }
            }
            else
            {
                model.ModifiedOn = dateTime;
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = GameService.CreateGameArticle(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/game/article", "/game/article");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }
    }
}
